package com.wx.wxapp.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.wx.wxapp.entity.Petimage;
import com.wx.wxapp.service.ImageService;
import net.minidev.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ImageController {
    public static final Logger logger = LoggerFactory.getLogger(ImageController.class);
    @Autowired
    private ImageService imageService;

    /*
    *   功能：上传用户发帖图片到服务器上
    *   参数：
    *       file:图片流
    *       cardId:帖子的id
    *       cardType:帖子类型
    * */
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public void uploadImage(@RequestParam("image")MultipartFile[] file,
                            @RequestParam("cardId")String cardId,@RequestParam("cardType")String cardType){
        System.out.println(file);
        System.out.println(cardId);
        for(int i=0;i<file.length;i++){
            imageService.UploadImage(file[i],cardId,cardType);
        }
    }
    /*
     *   功能：在用户更改宠物图片时，删除之前保存的图片
     *   参数：
     *       openid:用户的openid
     * */
    @RequestMapping(value = "/petDeleteImage",method = RequestMethod.GET)
    public void petDeleteImage(@RequestParam("openid")String openid){
        imageService.DeleteImage(openid);
    }
    /*
     *   功能：上传用户宠物图片到服务器上
     *   参数：
     *       file:图片流
     *       openid:用户的openid
     * */
    @RequestMapping(value = "/petUploadImage",method = RequestMethod.POST)
    public String petUploadImage(@RequestParam("image")MultipartFile[] files,@RequestParam("openid")String openid,HttpServletRequest request){
        Map<String, Object> value = new HashMap<>();
        for (int i=0;i<files.length;i++){
            try {
                String url = imageService.PetUploadImage(files[i],openid);
                logger.debug("图片路径{}",url);
                value.put("data", url);
                value.put("code", 0);
                value.put("msg", "图片上传成功");
            } catch (Exception e) {
                e.printStackTrace();
                value.put("code", 2000);
                value.put("msg", "图片上传成功");
            }
        }
        return JSONObject.toJSONString(value);
    }
    /*
     *   功能：获取用户宠物图片
     *   参数：
     *       openid:用户的openid
     * */
    @RequestMapping(value = "getPetImage",method = RequestMethod.GET)
    public List<Petimage> getPetImage(@RequestParam("openid")String openid){
        return imageService.GetPetImage(openid);
    }
    /*
     *   功能：获取帖子图片
     *   参数：
     *       cardId:帖子的Id
     *       cardType:帖子类型
     * */
    @RequestMapping(value = "getCardImage",method = RequestMethod.GET)
    public List<String> getCardImage(@RequestParam("cardId")String cardId,@RequestParam("cardType")String cardType){
        return imageService.GetCardImage(cardId,cardType);
    }
}
